package com.usian.controller;

import com.usian.pojo.Users;
import com.usian.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersService usersService;

    /**
     * 页面跳转
     */
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page) {
        return page;
    }

    @RequestMapping("/toaddUser")
    public String toRegister(){
        return "add";
    }

    /**
     * 添加用户
     */
    @RequestMapping("/addUser")
    public String addUser(Users users,Model model) {
        this.usersService.addUser(users);
        List<Users> list = this.usersService.listUser();
        model.addAttribute("list", list);
        return "list";
    }
    @RequestMapping("/deleteUser")
    public String deleteUser(Integer id,Model model) {
        this.usersService.deleteUser(id);

        List<Users> list = this.usersService.listUser();
        model.addAttribute("list", list);
        return "list";
    }

    @RequestMapping("/listUser")
    public String listUser(Model model) {
        List<Users> list = this.usersService.listUser();
        model.addAttribute("list", list);
        return "list";
    }

    @RequestMapping("/toupdate")
    public String  toupdate(Model model,Integer id) {
      Users up= usersService.toupdate(id);
        model.addAttribute("up", up);
        return "update";
    }
}